/**
* @module State
*/
define(function(require, exports, module) {

  function State(){
  	this.init();
  };

  State.prototype.init = function() {

    this.code = {
      init: [],
      remote: [],
      local: []
    };
    this.remote = false,
    this.loop = {};
    this.remoteVars = [];

	  this.loopdepth = 0;
    //this.conddepth = 0;
    //this.calldepth = 0;
  };

  State.prototype.addRemoteVar = function(name) {
    this.remoteVars.push(name);
  }

  State.prototype.removeRemoteVar = function(name) {
    var remoteVars = [];
    for( var i = 0; i < this.remoteVars.length; i++ ) {
      if( this.remoteVars[i] != name ) {
        remoteVars.push(remoteVars[i])
      }
    }
    this.remoteVars = remoteVars;
  }

  State.prototype.isRemote = function(name) {
    var remoteVars = this.getRemoteVars();

    for( var i = 0; i < remoteVars.length; i++ ) {
      if( remoteVars[i] == name ) {
        return true;
      }
    }

    return false;
  }

  State.prototype.getRemoteVars = function() {
    return this.getRemoteLoopVars(this.remoteVars);
  }

  State.prototype.getRemoteLoopVars = function(mergeArray) {
    var vars;
    if( mergeArray ) {
      vars = mergeArray;
    }
    else {
      vars = [];
    }
    var depth = this.loopdepth;

    while( depth > 0 ) {
      if( this.loop[depth].mode == 'both' ) {
        vars.push(this.loop[depth].item);
        depth = depth - 1;
      }
    }

    return vars;
  }

  exports.State = State;
});